/*
 * 系统项目名称：综合决策分析系统-version2.0
 * 文件名：ShopCompanyServiceImpl.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：yangjx
 * 修改时间：2016-3-11 下午1:45:34
 * 版本号：V1.1
 */
package com.ywt.cda.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ywt.cda.action.model.AreaModel;
import com.ywt.cda.action.model.PortsModel;
import com.ywt.cda.dao.Ana1stDataDao;
import com.ywt.cda.dao.AnaImportDataDao;
import com.ywt.cda.domain.*;
import com.ywt.cda.utils.DateUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.action.model.GoodsModel;
import com.ywt.cda.core.dao.page.Page;
import com.ywt.cda.dao.Ana2ndDataDao;
import com.ywt.cda.domain.enums.AnaTable;
import com.ywt.cda.domain.enums.DimensionFlag;
import com.ywt.cda.domain.enums.OrderByEnum;
import com.ywt.cda.domain.enums.ParamFlag;
import com.ywt.cda.domain.enums.ShopDimensionFlag;
import com.ywt.cda.service.ConfDataCacheService;
import com.ywt.cda.service.AreaService;
import com.ywt.cda.service.GovAnalysisService;
/**
 * （一句话功能简述）
 * <br>（功能详细描述）
 * @author lougf
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016-3-11 下午1:45:34
 */
@Service
public class AreaServiceImpl implements AreaService {

	@Resource
	private GovAnalysisService govAnalysisService;
	@Resource
	private Ana2ndDataDao ana2ndDataDao;
	@Resource
	private Ana1stDataDao ana1stDataDao;
	@Resource
	private AnaImportDataDao anaImportDataDao;
	@Resource
	private ConfDataCacheService cdc;
	private final String YYYY_MM="yyyy-MM";


	@Override
	public List<Ana1stData> queryExportData_1ST(GoodsModel model) {
		AnaTable table=AnaTable.TB_ANA_DATA_1ST_GOV_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_1ST_GOV_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("tableName", table.getTableName());
		params.put("code", DimensionFlag.EXPORT_COUNTRY.getCode());
		return ana1stDataDao.listSumNum(params);
	}

	@Override
	public List<AnaImportData> listImportByProvince(AreaModel model, String province) {
		AnaTable table=AnaTable.TB_ANA_IMPORT_DATA_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_IMPORT_DATA_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("tableName", table.getTableName());
		params.put("dimensionFlag", DimensionFlag.PROVINCE_TYPE.getCode());
		params.put("dimensionCode", province);
		return anaImportDataDao.listByProvince(params);
	}

	@Override
	public Long getAllProvinceSumNum(AreaModel model) {
		AnaTable table=AnaTable.TB_ANA_IMPORT_DATA_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_IMPORT_DATA_M;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("endDate", model.getEndTime_F());
		params.put("startDate", model.getStartTime_F());
		params.put("tableName", table.getTableName());
		params.put("dimensionFlag", DimensionFlag.PROVINCE_TYPE.getCode());
		return anaImportDataDao.getAllProvinceSumNum(params);
	}

	@Override
	public List<Ana2ndData> getTopNAreaData(GoodsModel model, int topN,
			OrderByEnum orderBy, DimensionFlag dimensionFlag) {
		Page page = new Page(1,topN);
		AnaTable table=AnaTable.TB_ANA_DATA_2ND_GOV_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_DATA_2ND_GOV_M;
		}
		List<Ana2ndData> datas = govAnalysisService.queryTotalDataWithPage(
				model.getStartTime_F(), model.getEndTime_F(),
				table, null,
				dimensionFlag.getCode(), orderBy.name(),"t.MAIN_CODE", page);
		return datas;
	}
	
	@Override
	public void toJsonArray(List<Ana2ndData> data,JSONArray jsonArray,OrderByEnum orderByEnum) {

		if (data != null) {
			for (int i = 0; i < data.size(); i++) {
				Ana2ndData ana2ndData = data.get(i);
				JSONObject nameValue = new JSONObject();

				nameValue.put("name", ana2ndData.getMainName());

				switch (orderByEnum) {
				case SUM_NUM:
					nameValue.put("value",String.valueOf(ana2ndData.getSumNum()));
					break;
				default:
					
					break;
				}
				

				jsonArray.add(nameValue);

			}

		}
	}

	@Override
	public void initDataMainName(List<Ana2ndData> data,
			DimensionFlag dimensionFlag)
	{

		  if(null != data && data.size() > 0){
	            for(Ana2ndData temp : data){
					ConfCountry country = cdc.getCountry(temp.getMainCode());
					if(null != country){
						temp.setMainName(country.getName());
					}
	            }
		  }
		
	}

	@Override
	public List<AnaImportData> getTopNAreaImortData(GoodsModel model, int topN, OrderByEnum orderBy, DimensionFlag dimensionFlag) {
		Page page = new Page(1,topN);
		AnaTable table=AnaTable.TB_ANA_IMPORT_DATA_D;
		if(YYYY_MM.equals(model.getStartTimePattern())){
			table=AnaTable.TB_ANA_IMPORT_DATA_M;
		}
		List<AnaImportData> datas = govAnalysisService.queryTotalImportDataWithPage(
				model.getStartTime_F(), model.getEndTime_F(),
				table, null,
				dimensionFlag.getCode(), orderBy.name(),"t.DIMENSION_CODE", page);
		return datas;
	}

	@Override
	public void initImportDataMainName(List<AnaImportData> data,
								 DimensionFlag dimensionFlag)
	{

		if(null != data && data.size() > 0){
			for(AnaImportData temp : data){
				temp.setMainCode(temp.getDimensionCode());
				if(null == temp.getMainCode()){
					temp.setMainCode(temp.getSecondCode());
				}
				ConfCountry country = cdc.getCountry(temp.getMainCode());
				if(null != country){
					temp.setMainName(country.getName());
				}
			}
		}

	}

	@Override
	public void initExportDataMainName(List<AnaExportData> data,
									   DimensionFlag dimensionFlag)
	{

		if(null != data && data.size() > 0){
			for(AnaExportData temp : data){
				temp.setMainCode(temp.getDimensionCode());
				if(null == temp.getMainCode()){
					temp.setMainCode(temp.getSecondCode());
				}
				ConfCountry country = cdc.getCountry(temp.getMainCode());
				if(null != country){
					temp.setMainName(country.getName());
				}
			}
		}

	}

	@Override
	public void toJsonArrayImport(List<AnaImportData> data,JSONArray jsonArray,OrderByEnum orderByEnum) {

		if (data != null) {
			for (int i = 0; i < data.size(); i++) {
				AnaImportData ana2ndData = data.get(i);
				JSONObject nameValue = new JSONObject();
				if(ana2ndData.getMainName()==null){
					ana2ndData.setMainName(ana2ndData.getMainCode());
				}
				nameValue.put("name", ana2ndData.getMainName());

				switch (orderByEnum) {
					case SUM_NUM:
						nameValue.put("value",String.valueOf(ana2ndData.getSumNum()));
						break;
					default:

						break;
				}


				jsonArray.add(nameValue);

			}

		}
	}

	@Override
	public void toJsonArrayExport(List<AnaExportData> data,JSONArray jsonArray,OrderByEnum orderByEnum) {

		if (data != null) {
			for (int i = 0; i < data.size(); i++) {
				AnaExportData ana2ndData = data.get(i);
				JSONObject nameValue = new JSONObject();

				nameValue.put("name", ana2ndData.getMainName());

				switch (orderByEnum) {
					case SUM_NUM:
						nameValue.put("value",String.valueOf(ana2ndData.getSumNum()));
						break;
					default:

						break;
				}


				jsonArray.add(nameValue);

			}

		}
	}



}
